package com.sesxh.project.knowledge.biz.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.google.common.collect.Lists;
import com.sesxh.frame.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.sesxh.frame.core.util.ZBeanUtil;
import com.sesxh.frame.core.util.ZDateUtil;
import com.sesxh.frame.core.util.ZObjectUtil;
import com.sesxh.frame.core.util.ZAlert;
import com.sesxh.frame.core.util.ZAssert;
import com.sesxh.frame.dao.ParamMap;
import com.sesxh.project.knowledge.biz.service.IKnowledgeService;
import com.sesxh.project.knowledge.biz.dao.KnowledgeMapper;
import com.sesxh.project.knowledge.biz.dto.ESContent;
import com.sesxh.project.knowledge.biz.model.Knowledge;
import java.util.stream.Stream;
@Service
public class KnowledgeServiceImpl implements IKnowledgeService{
 	@Autowired
	private KnowledgeMapper knowledgeMapper;
	
 	@Autowired
 	KnowledgeSearchService knowledgeSearchService;
 	
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createKnowledge(Knowledge knowledge) {
		ZBeanUtil.setCreateInfo(knowledge);
		ZBeanUtil.validateBean(knowledge);
		knowledgeMapper.insertSelective(knowledge);
		
		
		ESContent esContent=new ESContent();
		ZBeanUtil.copy(knowledge, esContent, true);
		knowledgeSearchService.createContent(esContent);
		
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<Knowledge> loadKnowledgeById(Long id) {
		if(id!=null){
			return Optional.ofNullable(knowledgeMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateKnowledge(Knowledge knowledge) {
		ZBeanUtil.setUpdatedInfo(knowledge);
		ZBeanUtil.validateBean(knowledge);
		knowledgeMapper.updateByPrimaryKeySelective(knowledge);
		
		Knowledge knowledgeNew=knowledgeMapper.selectByPrimaryKey(knowledge.getId());
		
		ESContent esContent=new ESContent();
		ZBeanUtil.copy(knowledgeNew, esContent, true);
		knowledgeSearchService.createContent(esContent);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteKnowledge(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		knowledgeMapper.deleteByPrimaryKey(id);
		
		knowledgeSearchService.deleteContent(id);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setKnowledgeStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		knowledgeMapper.updateStatus( id,  status) ;
		
		
		Knowledge knowledgeNew=knowledgeMapper.selectByPrimaryKey( id);
		ESContent esContent=new ESContent();
		ZBeanUtil.copy(knowledgeNew, esContent, true);
		knowledgeSearchService.createContent(esContent);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadKnowledgeDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		DataSet ds=DataSet.newDS2(knowledgeMapper.selectListCount(pm), knowledgeMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<Knowledge> loadKnowledgeByIds(List<Long> ids) {
		if(ZObjectUtil.isEmpty(ids)) {
			return Lists.newArrayList();
		}
		return knowledgeMapper.selectByids(ids);
	}
	
}